#include "testdialog.h"

TestDialog::TestDialog(QWidget *parent)
    : QDialog(parent)
{
	lbl = new QLabel(QObject::tr("Find &what:"));
	le = new QLineEdit;
	lbl->setBuddy(le);

	caseCheckBox = new QCheckBox(QObject::tr("Match &case"));
	backCheckBox = new QCheckBox(QObject::tr("Search &back"));

	searchBtn = new QPushButton(QObject::tr("&Search"));
	searchBtn->setDefault(true);
	searchBtn->setEnabled(false);

	closeBtn = new QPushButton(QObject::tr("Close"));

	connect(le,SIGNAL(textChanged(const QString&)),this,SLOT(enableSearchBtn(const QString&)));
	connect(searchBtn,SIGNAL(clicked()),this,SLOT(btnClicked()));
	connect(closeBtn,SIGNAL(clicked()),this,SLOT(close()));

	QHBoxLayout *topLayout = new QHBoxLayout;
	topLayout->addWidget(lbl);
	topLayout->addWidget(le);

	QVBoxLayout *leftLayout = new QVBoxLayout;
	leftLayout->addLayout(topLayout);
	leftLayout->addWidget(caseCheckBox);
	leftLayout->addWidget(backCheckBox);

	QVBoxLayout *rightLayout = new QVBoxLayout;
	rightLayout->addWidget(searchBtn);
	rightLayout->addWidget(closeBtn);
	rightLayout->addStretch();

	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->addLayout(leftLayout);
	mainLayout->addLayout(rightLayout);
	setLayout(mainLayout);

	setWindowTitle(QObject::tr("Search..."));
	setFixedHeight(sizeHint().height());
}

TestDialog::~TestDialog()
{

}

void TestDialog::btnClicked(){
	QString text = le->text();
	Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseInsensitive:Qt::CaseInsensitive;

	if(backCheckBox->isChecked()){
		emit findPrevious(text,cs);
	} else {
		emit findNext(text,cs);
	}
}

void TestDialog::enableSearchBtn(const QString &text){
	searchBtn->setEnabled(!text.isEmpty());
}
